<?php
/**
 * 
 * Path class file
 * 
 * @author		Stijn Van Minnebruggen
 * @version		1.0.0
 * 
 */

class Path
{

	/**
	 * Splitter for spaces, etc.
	 * 
	 * @var		array
	 */
	
	private $_splitter;
	
	
	/**
	 * Constructor method
	 * 
	 * @return	string
	 */
	
	public function __construct()
	{
		global $cosa;
		$this->_splitter = $cosa->system('path-splitter');
	}
	
	
	/**
	 * Create path name from string
	 * 
	 * @param	string	$value
	 * @param	string	$language
	 * @param	int		$item_id
	 * @return	string
	 */
	
	public function create($value, $table, $language, $item_id=false)
	{
		// required
			global $cosa, $db;
		
		// generate path
			$i = 2;
			$path = $this->transform($value);
			$exists = $this->_pathExists($path, $table, $language, $item_id);
			while($exists) {
				$path .= $this->_splitter.$i++;
				$exists = $this->_pathExists($path, $table, $language, $item_id);
			}
		
		// save path
			$this->_savePath($path, $table, $language, $item_id);
		
		// return path
			return $path;
		
	}
	
	
	/**
	 * Delete path from database
	 * 
	 * @param	string	$table
	 * @param	int		$item_id
	 * @return	bool
	 */
	
	public function delete($table, $item_id)
	{
		// required
			global $cosa, $db;
		
		// delete item
			$sql = "DELETE FROM ".$db->table('paths')." WHERE `table` = '".addslashes($table)."' AND item_id = '".addslashes($item_id)."'";
			$res = mysql_query($sql) or $cosa->oops('Error while deleting path.');
		
		// return bool
			return true;
		
	}
	
	
	/**
	 * Create url friendly path from string
	 * 
	 * @param	string	$value
	 * @return	string
	 */
	
	public function transform($value)
	{
		// to lowercase
			$v = $value;
			$v = strtolower($v);
		
		// change some basic chars
			$sr = array( 'a'	=> array('á','à','â','ä')
						,'e'	=> array('é','è','ê','ë')
						,'i'	=> array('í','ì','î','ï')
						,'u'	=> array('ú','ù','û','ü')
						,'c'	=> 'ç'
						);
			foreach($sr as $r => $s) $v = str_replace($s, $r, $v);
		
		// clean up other chars
			$v = preg_replace('([^a-z0-9\ \-]+)', '', $v);
		
		// replace spaces
			$v = str_replace(' ', $this->_splitter, trim($v));
		
		// trim double splitchars
			$d = $this->_splitter.$this->_splitter;
			while(strpos($v, $d)) $v = str_replace($d, $this->_splitter, $v);
		
		// return url friendly path
			return $v;
		
	}
	
	
	/**
	 * Check if path exists
	 * 
	 * @param	string	$path
	 * @param	string	$table
	 * @param	string	$language
	 * @param	int		$item_id
	 * @return	bool
	 */
	
	private function _pathExists($path, $table, $language_id, $item_id)
	{
		// required
			global $cosa, $db;
		
		// check path in database
			$sql = "SELECT id FROM ".$db->table('paths')." WHERE `table` = '".addslashes($table)."' AND language_id = '".addslashes($language_id)."' AND item_id <> '".addslashes($item_id)."' AND path = '".addslashes($path)."' LIMIT 1";
			$res = mysql_query($sql) or $cosa->oops('Error while creating path.');
			$exists = mysql_num_rows($res);
		
		// return bool
			return $exists;
		
	}
	
	
	/**
	 * Save path in path database
	 * 
	 * @param	string	$path
	 * @param	string	$table
	 * @param	string	$language
	 * @param	int		$item_id
	 * @return	bool
	 */
	
	private function _savePath($path, $table, $language_id, $item_id)
	{
		// required
			global $cosa, $db;
			if(!$path || !$table || !$language_id || !$item_id) $cosa->kill('Could not save path.');
		
		// check if exists
			$sql = "SELECT id FROM ".$db->table('paths')." WHERE `table` = '".addslashes($table)."' AND language_id = '".addslashes($language_id)."' AND item_id = '".addslashes($item_id)."' LIMIT 1";
			$res = mysql_query($sql) or $cosa->kill('Error while saving path.');
			$row = mysql_fetch_array($res);
			$path_id = mysql_num_rows($res) ? $row['id'] : false;
		
		// save path
			$sql = ($path_id) ? "UPDATE ".$db->table('paths') : "INSERT INTO ".$db->table('paths');
			$sql .= " SET	`table`			= '".addslashes($table)."'
							,language_id	= '".addslashes($language_id)."'
							,item_id		= '".addslashes($item_id)."'
							,path			= '".addslashes($path)."'
					";
			if($path_id) $sql .= " WHERE id = '".addslashes($path_id)."'";
			mysql_query($sql) or $cosa->kill('Error while creating path.');
		
		// return bool
			return true;
		
	}
	
	
	/**
	 * Get path from id
	 * 
	 * @param	string	$table
	 * @param	int		$id
	 * @return	string
	 */
	
	public function get($table, $item_id)
	{
		global $cosa, $db, $lang;
		$sql = "SELECT path FROM ".$db->table('paths')." WHERE `table` = '".addslashes($table)."' AND language_id = '".addslashes($lang->id)."' AND item_id = '".addslashes($item_id)."' LIMIT 1";
		$res = mysql_query($sql) or $cosa->oops('Error while fetching path.', 'db');
		if(!mysql_num_rows($res)) return false;
		$row = mysql_fetch_array($res);
		return stripslashes($row['path']);
	}
	
	
	/**
	 * Change an item id (usefull for version controlled modules)
	 * 
	 * @param	string	$table
	 * @param	string	$language_id
	 * @param	int		$old_item_id
	 * @param	int		$new_item_id
	 * @return	bool
	 */
	
	public function updateItemId($table, $language_id, $old_item_id, $new_item_id)
	{
		global $cosa, $db;
		$sql = "UPDATE ".$db->table('paths')." SET item_id = '".addslashes($new_item_id)."' WHERE item_id = '".addslashes($old_item_id)."' AND `table` = '".addslashes($table)."' AND language_id = '".addslashes($language_id)."'";
		$res = mysql_query($sql) or $cosa->oops('Error while updating path');
		return true;
	}
	
	
	
}

?>